package com.greenhouse.business.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.greenhouse.business.domain.BizDevice;
import com.greenhouse.business.domain.BizDeviceGroup;
import com.greenhouse.business.service.IBizDeviceGroupService;
import com.greenhouse.business.service.IBizDeviceService;
import com.greenhouse.common.annotation.Log;
import com.greenhouse.common.core.controller.BaseController;
import com.greenhouse.common.core.domain.AjaxResult;
import com.greenhouse.common.core.page.TableDataInfo;
import com.greenhouse.common.enums.BusinessType;
import com.greenhouse.common.utils.poi.ExcelUtil;

/**
 * 设备管理Controller
 * 
 * @author greenhouse
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/business/device")
public class BizDeviceController extends BaseController {
	@Autowired
	private IBizDeviceService bizDeviceService;

	@Autowired
	private IBizDeviceGroupService bizDeviceGroupService;

	/**
	 * 查询设备管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:device:list')")
	@GetMapping("/list")
	public TableDataInfo list(BizDevice bizDevice) {
		startPage();
		List<BizDevice> list = bizDeviceService.selectBizDeviceList(bizDevice);
		return getDataTable(list);
	}

	@GetMapping("/listDeviceByGroupId/{groupId}")
	public TableDataInfo listDeviceByGroupId(@PathVariable("groupId") Long groupId) {
		startPage();
		BizDeviceGroup bizDeviceGroup = new BizDeviceGroup();
		bizDeviceGroup.setGroupId(groupId);
		List<BizDeviceGroup> groups = bizDeviceGroupService.selectBizDeviceGroupList(bizDeviceGroup);
		if (groups.size() == 0) {
			return getDataTable(new ArrayList<BizDevice>());
		}
		Long[] ids = new Long[groups.size()];
		for (int i = 0; i < groups.size(); i++) {
			ids[i] = groups.get(i).getDeviceId();
		}

		List<BizDevice> list = bizDeviceService.selectBizDeviceByIds(ids);
		return getDataTable(list);
	}

	/**
	 * 导出设备管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:device:export')")
	@Log(title = "设备管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BizDevice bizDevice) {
		List<BizDevice> list = bizDeviceService.selectBizDeviceList(bizDevice);
		ExcelUtil<BizDevice> util = new ExcelUtil<BizDevice>(BizDevice.class);
		return util.exportExcel(list, "device");
	}

	/**
	 * 获取设备管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:device:query')")
	@GetMapping(value = "/{deviceId}")
	public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId) {
		return AjaxResult.success(bizDeviceService.selectBizDeviceById(deviceId));
	}

	/**
	 * 新增设备管理
	 */
	@PreAuthorize("@ss.hasPermi('business:device:add')")
	@Log(title = "设备管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BizDevice bizDevice) {
		return toAjax(bizDeviceService.insertBizDevice(bizDevice));
	}

	/**
	 * 修改设备管理
	 */
	@PreAuthorize("@ss.hasPermi('business:device:edit')")
	@Log(title = "设备管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BizDevice bizDevice) {
		return toAjax(bizDeviceService.updateBizDevice(bizDevice));
	}

	/**
	 * 删除设备管理
	 */
	@PreAuthorize("@ss.hasPermi('business:device:remove')")
	@Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
	public AjaxResult remove(@PathVariable Long[] deviceIds) {
		return toAjax(bizDeviceService.deleteBizDeviceByIds(deviceIds));
	}

}
